package com.gexin.model.system;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.cache.RedisKeyConst;
import com.gexin.common.utils.SQLUtils;
import com.gexin.model.system.base.BaseAddrAreas;
import com.jfinal.plugin.activerecord.Db;

import java.util.List;

/**
 * 业务名称:  t_addr_areas(行政区域县区信息表) 
 */
@SuppressWarnings("serial")
public class AddrAreas extends BaseAddrAreas<AddrAreas> {
	public static final AddrAreas dao = new AddrAreas();

	/**
	 * 根据区县ID查询
	 * @param id
	 * @return
	 */
	public AddrAreas findByAreaId(String id) {
		return dao.findFirst("select * from t_addr_areas where areaid=?",id);
	}

	public List<AddrAreas>  getAreasByCitysId(String city_id){
		SQLUtils sql =new SQLUtils("select *  from  t_addr_areas where 1=1 ");
		sql.whereEquals(FIELD_CITYID, city_id);
		sql.append(" order by id asc ");
		return dao.find(sql.toString());
	}

	/**
	 * fang 2016-07-06
	 * 存储所有的地区信息
	 */
	public List<AddrAreas>getAllAreas(){
		List<AddrAreas>  list_area = CacheUtils.get(RedisKeyConst.Address_Area);
		if(list_area==null){
			list_area=dao.find("select *  from t_addr_areas  order by  id  asc");
			CacheUtils.set(RedisKeyConst.Address_Area, list_area,365*24*3600);//缓存一年
		}
		return list_area;
	}

	/**
	 * fang 2016-07-06
	 * 往   tempalte_area  插入数据
	 *
	 */
	public int insertData(String shop_id,String area_id,int template_id){
		String sql=" INSERT INTO t_template_area(shop_id,area_id,template_id)  VALUES (?, ?,?)";
		return Db.update(sql,shop_id,area_id,template_id);

	}

	/**
	 * fang  2016-07-08
	 * 根据 city_id 拿到所有的县
	 *
	 */
	public List<AddrAreas> getAreasByCityId(String city_id){
		SQLUtils sql =new SQLUtils("select *  from  t_addr_areas where 1=1 ");
		sql.whereEquals(FIELD_CITYID, city_id);
		sql.append(" order by id asc ");
		return dao.find(sql.toString());
	}


	/**
	 * fang  2016-07-12
	 * 根据areaid  扎到area
	 * @param area_id
	 * @return
	 */
	public AddrAreas getAreaByAreaId(String area_id){
		String sql=" select *  from t_addr_areas where areaid =?";
		return dao.findFirst(sql,area_id);
	}

}
